﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 图片裁剪压缩工具
{
    public class Utils
    {
        private static readonly char[] hexDigits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        public static string ToHexString(byte[] buff)
        {
            int len = buff.Length;
            char[] array = new char[len * 2];
            for (int i = 0; i < len; i++)
            {
                int x = buff[i];
                array[i * 2] = hexDigits[x >> 4];
                array[i * 2 + 1] = hexDigits[x & 0xf];
            }
            return new string(array);
        }

     
        public static string FileContentSha1(string fileName)
        {
            using SHA1 sha = SHA1.Create();
            using FileStream fileStream = File.OpenRead(fileName);
            byte[] buff = sha.ComputeHash(fileStream);
            return ToHexString(buff);
        }

        public static string StringSha1(string s)
        {
            using SHA1 sha = SHA1.Create();
            byte[] buff = sha.ComputeHash(Encoding.UTF8.GetBytes(s));
            return ToHexString(buff);
        }

        public static string FileInfoSha1(string fileName)
        {
            var fileInfo = new FileInfo(fileName);
            return StringSha1(fileName + "`!@#$%^&*()-=" + fileInfo.LastWriteTimeUtc.ToString());
        }
    }
}
